import cv2
import numpy as np
import matplotlib.pyplot as plt

def show_fft_symmetry(image_path, roi=None):
    """
    仅做 FFT，显示对称亮点频谱图
    :param image_path: 条纹图路径
    :param roi: [x,y,w,h]，若为 None，则手动框选
    """
    # 1. 读图
    img = cv2.imread(image_path, 0)
    if img is None:
        raise FileNotFoundError(image_path)

    # 2. 选取 ROI
    if roi is None:
        roi = cv2.selectROI('select ROI', img)
        cv2.destroyAllWindows()
    x, y, w, h = roi
    roi_img = img[y:y + h, x:x + w]

    # 3. FFT 并中心化
    f = np.fft.fft2(roi_img)
    fshift = np.fft.fftshift(f)
    magnitude = 20 * np.log(np.abs(fshift) + 1)   # 对数压缩，便于显示

    # 4. 画图
    plt.figure(figsize=(6, 6))
    plt.imshow(magnitude, cmap='gray')
    plt.title('FFT magnitude (log)')
    plt.axis('off')
    plt.show()
    return magnitude


# ------------------- demo --------------------
if __name__ == '__main__':
    image_path = '../img/pic3.jpg'  # 改成你的图片
    show_fft_symmetry(image_path)